package com.itheima.mp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.entity.Address;
import com.itheima.mp.mapper.UserMapper;
import com.itheima.mp.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public UserVO getUserWithAddressById(Long id) {
        // 查询用户信息
        User user = this.getById(id);
        // 查询地址信息
        List<Address> list = Db.lambdaQuery(Address.class).eq(Address::getUserId, id).list();
        // 合并返回
        UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
        userVO.setAddressList(list);
        return userVO;
    }


    @Override
    public List<UserVO> queryUsers(UserQuery query) {
        //        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.like(StringUtils.hasLength(query.getName()), User::getUsername, query.getName())
//                    .eq(query.getStatus()!=null, User::getStatus, query.getStatus())
//                    .gt(query.getMinBalance()!=null, User::getBalance,query.getMinBalance())
//                    .lt(query.getMaxBalance()!=null, User::getBalance, query.getMaxBalance());
//        List<User> list = this.list(queryWrapper);
//        return BeanUtil.copyToList(list, UserVO.class);

        List<User> list = this.lambdaQuery()
                .like(StringUtils.hasLength(query.getName()), User::getUsername, query.getName())
                .eq(query.getStatus() != null, User::getStatus, query.getStatus())
                .gt(query.getMinBalance() != null, User::getBalance, query.getMinBalance())
                .lt(query.getMaxBalance() != null, User::getBalance, query.getMaxBalance())
                .list();
        return BeanUtil.copyToList(list, UserVO.class);
    }


    @Override
    public void deduct(Long id, Integer money) {
        // 查询用户
        User user = this.getById(id);
        if(user == null){
            return;
        }
//        //判断用户的状态
//        Integer status = user.getStatus();
//        if(!Objects.equals(status, 1)){
//            log.error("账户被冻结");
//            return;
//        }
        // 判断用户的账户余额
        Integer balance = user.getBalance();
        if(balance < money){
            log.error("账户余额不足");
            return;
        }
        // 扣减  update user set balance= balance - #{money} where id = #{id}
        LambdaUpdateWrapper<User> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.setSql("balance= balance -" + money);
        queryWrapper.eq(User::getId, id);
        this.update(queryWrapper);
    }
}